/*
 * Copyright 2008 Jeff Dwyer
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.rpi.bt.client.domain.commands;

import java.io.Serializable;

import com.allen_sauer.gwt.log.client.Log;
import com.apress.progwt.client.domain.User;
import com.apress.progwt.client.domain.commands.AbstractCommand;
import com.apress.progwt.client.domain.commands.CommandService;
import com.apress.progwt.client.exception.SiteException;

import edu.rpi.bt.client.domain.Comment;
import edu.rpi.bt.client.domain.Destination;

public class SaveCommentCommand extends AbstractCommand implements
        Serializable {
    /*
     * private long destinationID; private int rating; private long
     * commentID; private String userComment; private long authorID;
     */

    private long destinationID;
    private long commentID;
    private long authorID;
    private Comment original;
    private User user;
    private Comment toSave;
    private long savedCommentID;

    public SaveCommentCommand() {
    }

    public SaveCommentCommand(Destination destination, Comment comment) {
        super(destination, comment);

        this.commentID = comment.getId();

        this.destinationID = destination.getId();
        original = comment;
      //  this.user = user;
        //this.authorID = user.getId();

        toSave = original;
    }

    @Override
    public String toString() {
        return "SaveCommentCommand DestID " + destinationID + " comment "
                + commentID + " comment " + toSave.getComment();
    }

    public boolean haveYouSecuredYourselfAndFilteredUserInput() {
        return true;
    }

    public void execute(CommandService commandService)
            throws SiteException {
        System.out.println("comment In execute");
        // User author = commandService.get(User.class, authorID);

        // User author = commandService.get(User.class, 1);
        System.out.println("In execute 2...");
        // toSave=original;
        // toSave = commandService.get(Destination.class, destinationID);

        System.out.println("In execute 2a ");
        

            System.out.println("Name :" + toSave.getComment() + " user "
                    + toSave.getUser());
     //        toSave.setUser(user);
            commandService.assertUserIsAuthenticated(toSave.getUser());
        

        System.out.println("In execute 2b ");
        Log.debug("SaveCommentCommand " + destinationID + " loaded: "
                + toSave + " original: " + original);

        // String xssFiltered =
        // commandService.filterHTML(original.getDescription());

        // toSave.setNotes(xssFiltered);

        System.out.println("In execute 3");
        commandService.save(toSave);
        this.savedCommentID = toSave.getId();
        System.out.println("In execute 4");
    }

    public long getSavedCommentID() {
        return savedCommentID;
    }

    /**
     * used in testing accessor
     * 
     * @return
     */
    public Comment getToSave() {
        return toSave;
    }

    /*
     * public void execute(CommandService commandService) throws
     * SiteException {
     * 
     * Destination destination = (Destination) commandService.get(
     * Destination.class, getDestinationID()); Comment comment = (Comment)
     * commandService.get( Comment.class, getCommentID()); User author =
     * commandService.get(User.class, authorID);
     * 
     * 
     * // assertUserIsAuthenticated(Comment.getUser());
     * 
     * commandService.assertUserIsAuthenticated(comment.getUser());
     * 
     * Log.debug("SaveCoomentCommand " + toString());
     * Log.debug("SaveCommentCommand Destination " + destination +
     * " comment " + comment);
     * 
     * if (comment != null) { Log.debug("comments: " +
     * comment.getComment()); }else{ comment = new Comment(author,
     * destination, getUserComment(), getRating());
     * commandService.save(comment);
     * 
     * }
     * 
     * 
     * 
     * 
     * }
     */

    public long getDestinationID() {
        return destinationID;
    }

    /*
     * public int getRating() { return rating; }
     */
    public long getCommentID() {
        return commentID;
    }

    public Comment getUserComment() {
        return this.toSave;
    }

}
